public class Test20_1 {
    public static boolean isNumeric(String string) {
        if (string == null || string.length() < 1) return false;
        int index = 0;
        //先判断整数
        if (string.charAt(index) == '+' || string.charAt(index) == '-') {
            index++;
        }
        //已经到达字符串的末尾
        if (index >= string.length()) return false;

        //判断标准
        boolean numeric = true;
        //找到数字部分的下一个位置
        index = scanDigits(string, index);

        //如果没有到达末尾
        if (index < string.length()) {
            //如果是小数点
            if (string.charAt(index) == '.') {
                index++;
                index = scanDigits(string, index);
                //如果已经达到了字符的末尾
                if (index >= string.length()) numeric = true;
                //如果还没有到达，判断是否为指数
                else if (index < string.length() && (string.charAt(index) == 'e' || string.charAt(index) == 'E'))
                    //判断指数是否合法
                    numeric = isExponential(string,index);
                //不是返回false
                else numeric = false;
            }return numeric;
        }
        else return true;
    }

    //扫描字符串中数字部分
    public static int scanDigits(String string, int index) {
        while (index < string.length() && string.charAt(index) >= '0' && string.charAt(index) <= '9')
            index++;
        return index;
    }

    /**
     * 判断指数部分是否合法
     * @param string
     * @param index
     * @return
     */
    public static boolean isExponential(String string, int index) {
        if (index >= string.length() || (string.charAt(index) != 'e' && string.charAt(index) != 'E')) {
            return false;
        }
        index ++;
        if (index >= string.length()) return  false;

        if (string.charAt(index) == '+' ||string.charAt(index) == '-'){
            index ++;
        }
        if (index >= string.length()) return false;

        index = scanDigits(string,index);

        return index >= string.length();
    }

    public static void main(String[] args) {
        System.out.println(isNumeric("100") + "[" + true + "]");
        System.out.println(isNumeric("123.45e+6") + "[" + true + "]");
        System.out.println(isNumeric("+500") + "[" + true + "]");
        System.out.println(isNumeric("5e2") + "[" + true + "]");
        System.out.println(isNumeric("1.79769313486232E+308") + "[" + true + "]");
        System.out.println(isNumeric("12e") + "[" + false + "]");
        System.out.println(isNumeric("1a3.14") + "[" + false + "]");
        System.out.println(isNumeric("1+23") + "[" + false + "]");
        System.out.println();
    }
}